﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace MasApp.BLL
{
    /// <summary>
    ///Search 的摘要说明
    /// </summary>
    public class Search
    {
        public Search()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// levelmissionid+ ap.Indicatorid;返回是否存在评级记录
        /// </summary>
        /// <param name="ap"></param>
        /// <returns></returns>
        public static bool ExistsApplevel(Model.applevel ap)
        {
            string s = "Select count(*) From applevel Where levelmissionid=" + ap.Levelmissionid
                + " And indicatorid =" + ap.Indicatorid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s)) > 0 ? true : false;
        }
        public static Model.model SearchModel(int modelid)
        {
            string s = "select * From [model] Where modelid =" + modelid;
            Model.model mod = new MasApp.Model.model();
            DataTable dt = DAL.Search.ExecuteTable(s);
            mod.Modelname = dt.Rows[0]["modelname"].ToString();
            mod.Target = dt.Rows[0]["target"].ToString();
            return mod;
        }
        /// <summary>
        /// 超找用户Email
        /// </summary>
        /// <returns></returns>
        public static string SearchEmailAddr()
        {
            string s = "Select email from users where userid = " + HttpContext.Current.Session["userid"].ToString();
            return DAL.Search.ExecuteScalar(s).ToString();
        }
        /// <summary>
        /// 查找结果集
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable SearchResult(int projectid)
        {
            string s = "SELECT     result.* "
                + " FROM         result"
                + " WHERE     (projectid = " + projectid + ")";
            return DAL.Search.ExecuteTable(s);
        }
        /// <summary>
        /// Weightmissionid + " And indicatorid 返回单个 indicatororder 
        /// </summary>
        /// <param name="aio"></param>
        /// <returns></returns>
        public static int SearchAppIndicatorOrder(Model.appindicatororder aio)
        {
            string s = "Select indicatororder From appindicatororder Where "
                + " weightmissionid = " + aio.Weightmissionid + " And indicatorid = " + aio.Indicatorid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s));
        }
        /// <summary>
        /// aio.Weightmissionid;+ aio.Indicatorid;
        /// </summary>
        /// <param name="aio"></param>
        /// <returns></returns>
        public static bool ExistsAppIndicatorOrder(Model.appindicatororder aio)
        {
            string s = "Select Count(appindicatororderid) From appindicatororder Where "
                + " weightmissionid = " + aio.Weightmissionid + " And indicatorid = " + aio.Indicatorid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s)) > 0 ? true : false;
        }
        /// <summary>
        /// aio.Weightmissionid; 
        /// </summary>
        /// <param name="aio"></param>
        /// <returns></returns>
        public static bool ExistsAppIndicatorOrders(Model.appindicatororder aio)
        {
            string s = "Select Count(appindicatororderid) From appindicatororder Where "
                + " weightmissionid = " + aio.Weightmissionid + " And indicatorid = " + aio.Indicatorid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s)) > 0 ? true : false;
        }
        /// <summary>
        ///  aro.Ruleid + " AND weightmissionid返回单个ruleorder
        /// </summary>
        /// <param name="aro"></param>
        /// <returns></returns>
        public static int SearchAppRuleOrder(Model.appruleorder aro)
        {
            string s = "Select ruleorder From appruleorder Where "
                + " ruleid = " + aro.Ruleid + " AND weightmissionid =" + aro.Weightmissionid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s));
        }
        /// <summary>
        /// aro.Weightmissionid; + aro.Ruleid;
        /// </summary>
        /// <param name="aro"></param>
        /// <returns></returns>
        public static bool ExistsAppRuleOrder(Model.appruleorder aro)
        {
            string s = "Select Count(appruleorderid) From appruleorder Where "
                + " weightmissionid = " + aro.Weightmissionid + " And ruleid =" + aro.Ruleid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s).ToString()) > 0 ? true : false;
        }
        /// <summary>
        /// aro.Weightmissionid; 
        /// </summary>
        /// <param name="aro"></param>
        /// <returns></returns>
        public static bool ExistsAppRuleOrders(Model.appruleorder aro)
        {
            string s = "Select Count(appruleorderid) From appruleorder Where "
                + " weightmissionid = " + aro.Weightmissionid + " And ruleid = " + aro.Ruleid;
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s).ToString()) > 0 ? true : false;
        }
        /// <summary>
        /// 通过projectid返回levelmissionid列表
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable LevelMissionIDs(int projectid)
        {
            return DAL.Search.LevelMissionIDs(projectid);
        }
        /// <summary>
        /// 通过indicatorid返回ruleid
        /// </summary>
        /// <param name="indicatorid"></param>
        /// <returns></returns>
        public static int RuleID(int indicatorid)
        {
            return DAL.Search.RuleID(indicatorid);
        }
        /// <summary>
        /// 通过项目ID返回权重值列表
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable WeightValue(int projectid)
        {
            return DAL.Search.WeightValue(projectid);
        }
        /// <summary>
        /// 通过项目ID返回对应的项目任务是否全部完成
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static bool MissionAllFinish(int projectid)
        {
            DataTable dt = DAL.Search.LevelMissionState(projectid);
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[0].ToString().Equals("在做的") || dr[0].ToString().Equals("未做的"))
                {
                    return false;
                }
            }
            dt.Clear();
            dt = DAL.Search.WeightMissionState(projectid);
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[0].ToString().Equals("在做的") || dr[0].ToString().Equals("未做的"))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 通过Applevel模型返回等级ID
        /// </summary>
        /// <param name="appl"></param>
        /// <returns></returns>
        public static int LevelIDinApplevel(Model.applevel appl)
        {
            return DAL.Search.LevelIDinApplevel(appl);
        }
        /// <summary>
        /// 通过Applevel模型返回评级ID（是否存在记录）
        /// </summary>
        /// <param name="appl"></param>
        /// <returns></returns>
        public static int HaveAppLevelID(Model.applevel appl)
        {
            return DAL.Search.HaveAppLevelID(appl);
        }
        /// <summary>
        /// 通过评级任务ID返回等级列表
        /// </summary>
        /// <param name="levelmissionid"></param>
        /// <returns></returns>
        public static DataTable Level2(int levelmissionid)
        {
            return MasApp.DAL.Search.Level2(levelmissionid);
        }
        /// <summary>
        /// 通过方法ID返回等级列表
        /// </summary>
        /// <param name="methodid"></param>
        /// <returns></returns>
        public static DataTable Level(int methodid)
        {
            return MasApp.DAL.Search.Level(methodid);
        }
        /// <summary>
        /// 通过model.levelmission 找到用户角色id
        /// </summary>
        /// <param name="lm"></param>
        /// <returns></returns>
        public static int LevelMissionActorRoleID(Model.levelmission lm)
        {
            return MasApp.DAL.Search.LevelMissionActorRoleID(lm);
        }
        /// <summary>
        /// 通过任务ID查找等级任务状态
        /// </summary>
        /// <param name="levelmissionid"></param>
        /// <returns></returns>
        public static string SearchLevelMissionState(int levelmissionid)
        {
            return DAL.Search.SearchLevelMissionState(levelmissionid);
        }

        /// <summary>
        /// 通过任务ID查找权重任务状态
        /// </summary>
        /// <param name="weightmissionid"></param>
        /// <returns></returns>
        public static string SearchWeightMissionState(int weightmissionid)
        {
            return DAL.Search.SearchWeightMissionState(weightmissionid);
        }
        /// <summary>
        /// 通过model.weight 查找权值
        /// </summary>
        /// <param name="we"></param>
        /// <returns></returns>
        public static float SearhWeightValue(Model.weight we)
        {
            return DAL.Search.SearchWeightValue(we);
        }
        /// <summary>
        /// 通过用户名，projectid查找在评权任务表是否存在记录，返回记录条数
        /// </summary>
        /// <param name="username"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchHaveUsernameInWeightmission(string username, int projectid)
        {
            return MasApp.DAL.Search.SearchHaveUsernameInWeightmission(username, projectid);
        }
        /// <summary>
        /// 通过用户名，projectid查找在评级任务表是否存在记录，返回记录条数
        /// </summary>
        /// <param name="username"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchHaveUsernameInLevelmission(string username, int projectid)
        {
            return DAL.Search.SearchHaveUsernameInLevelmission(username, projectid);
        }
        /// <summary>
        /// 通过projectid查找weightmissionid
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchWeightMissionID(int projectid)
        {
            return DAL.Search.SearchWeightMissionID(projectid);
        }
        public static DataTable SearchWeightMissionIDS(int projectid)
        {
            string s = "SELECT   weightmissionid "
                + " FROM         weightmission "
                + " WHERE     (projectid = " + projectid + ")";
            return DAL.Search.ExecuteTable(s);
        }
        /// <summary>
        /// 通过projectid查找准则ID以及名称
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable SearchRuleIDandName(int projectid)
        {
            return DAL.Search.SearchRuleIDandName(projectid);
        }
        /// <summary>
        /// 查找项目赋权方式，返回“直接赋权”或者“序关系法”
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static string SearchProjectWeightType(int projectid)
        {
            return DAL.Search.SearchProjectWeightType(projectid);
        }
        /// <summary>
        /// 查找用户名是否存在的状态
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool HaveUsername(string username)
        {
            return MasApp.DAL.Search.HaveUsername(username);
        }
        public static DataSet SearchManaUsers()
        {
            return MasApp.DAL.Search.SearchManaUsers();
        }
        public static DataSet SearchOrg()
        {
            return MasApp.DAL.Search.SearchOrg();
        }
        /// <summary>
        /// 通过用户名从用户表返回组织ID
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static int SearchOrgID(string username)
        {
            return MasApp.DAL.Search.SearchOrgID(username);
        }
        /// <summary>
        /// 通过用户ID返回用户角色ID、名称列表
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static DataSet SearchRoleIDandName(int userid)
        {
            return MasApp.DAL.Search.SearchRoleIDandName(userid);
        }
        /// <summary>
        /// 通过组织ID返回可使用的评估方法ID、名称列表
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchAvailMethodIDandName(int orgid)
        {
            return MasApp.DAL.Search.SearchAvailMethodIDandName(orgid);
        }
        /// <summary>
        /// 通过组织ID返回评估方法ID、名称列表
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchMethodIDandName(int orgid)
        {
            return MasApp.DAL.Search.SearchMethodIDandName(orgid);
        }
        /// <summary>
        /// 通过组织ID返回可使用的模型ID、名称列表
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchAvailModelIDandName(int orgid)
        {
            return MasApp.DAL.Search.SearchAvailModelIDandName(orgid);
        }
        /// <summary>
        /// 通过组织ID返回模型ID、名称列表
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchModelIDandName(int orgid)
        {
            return MasApp.DAL.Search.SearchModelIDandName(orgid);
        }
        /// <summary>
        /// 通过用户名返回用户ID
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static int SearchUserID(string username)
        {
            return MasApp.DAL.Search.SearchUserID(username);
        }
        /// <summary>
        /// 通过项目ID返回项目模型
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static Model.project SearchOneProject(int projectid)
        {
            return MasApp.DAL.Search.SearchOneProject(projectid);
        }
        /// <summary>
        /// 通过模型ID返回准则ID列表
        /// </summary>
        /// <param name="modelid"></param>
        /// <returns></returns>
        public static DataSet SearchRuleID(int modelid)
        {
            return MasApp.DAL.Search.SearchRuleID(modelid);
        }
        /// <summary>
        /// 通过用户名返回真实性名
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static string SearchUserRealName(string username)
        {
            return MasApp.DAL.Search.SearchUserRealName(username);
        }
        /// <summary>
        /// 通过modelid查找准则ID以及名称
        /// 
        /// </summary>
        /// <param name="modelid"></param>
        /// <returns></returns>
        public static DataTable SearchRuleIDsandNames(int modelid)
        {
            return MasApp.DAL.Search.SearchRuleIDsandNames(modelid);
        }
        /// <summary>
        /// 通过projectid查找指标ID及名称
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        /*public static DataTable SearchIndicatorIDandName(int projectid)
        {
            int modelid = app.DAL.Search.SearchModelID(projectid);
            int ruleid = app.DAL.Search.SearchRuleID(modelid);
            DataTable dt = app.DAL.Search.SearchIndicatorIDsandNames(ruleid);
            return dt;
        }*/
        /// <summary>
        /// 通过Ruleid查找指标ID及名称
        /// </summary>
        /// <param name="ruleid"></param>
        /// <returns></returns>
        public static DataTable SearchIndicatorIDsandNames(int ruleid)
        {
            return MasApp.DAL.Search.SearchIndicatorIDsandNames(ruleid);
        }
        /// <summary>
        /// 通过项目ID返回评估方法ID
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchMethodID(int projectid)
        {
            return MasApp.DAL.Search.SearchMethodID(projectid);
        }
        /// <summary>
        /// 通过项目ID返回模型ID
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchModelID(int projectid)
        {
            return MasApp.DAL.Search.SearchModelID(projectid);
        }
        public static int SearchRuleNumberByMisid(int weightmissionid)
        {
            string s = "SELECT  COUNT([rule].ruleid)"
                    + " FROM   project INNER JOIN"
                    + "  weightmission ON project.projectid = weightmission.projectid INNER JOIN"
                    + " [rule] ON project.modelid = [rule].modelid"
                    + " WHERE  (weightmission.weightmissionid =" + weightmissionid + ")";
            return Convert.ToInt32(DAL.Search.ExecuteScalar(s).ToString());
        }
        /// <summary>
        /// 通过projectid查找准则个数
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchRuleNumbers(int projectid)
        {
            return MasApp.DAL.Search.SearchRuleNumbers(projectid);
        }
        /// <summary>
        /// 通过modelid插好准则个数
        /// </summary>
        /// <param name="modelid"></param>
        /// <returns></returns>
        public static int SearchRuleNumber(int modelid)
        {
            return MasApp.DAL.Search.SearchRuleNumber(modelid);
        }
        /// <summary>
        /// 通过准则ID返回对应的指标的数目
        /// </summary>
        /// <param name="ruleid"></param>
        /// <returns></returns>
        public static int SearchIndicatorNumber(int ruleid)
        {
            return MasApp.DAL.Search.SearchIndicatorNumber(ruleid);
        }
        /// <summary>
        /// 通过项目ID
        /// 返回权重类型
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static string SearchWeighttype(int projectid)
        {
            return MasApp.DAL.Search.SearchWeighttype(projectid);
        }
        /// <summary>
        /// 通过组织ID查找用户ID，用户名，真实姓名列表，通过用户ID降序
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchUsersIDNameName(int orgid)
        {
            return MasApp.DAL.Search.SearchUsersIDNameName(orgid);
        }
    }
}